package com.neusoft.neuiotms.dcam.common.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.auth.model.AuthOganization;
import com.neusoft.bizcore.auth.repository.AuthOganizationRepository;
import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcam.common.dto.PrinterDTO;
import com.neusoft.neuiotms.dcam.common.model.Printer;
import com.neusoft.neuiotms.dcam.common.repository.PrinterRepository;

@Component
public class PrinterConverter extends AbstractConverter<Printer, PrinterDTO> {

    @Autowired
    private PrinterRepository printerRepository;
    @Autowired
    private AuthOganizationRepository authOganizationRepository;

    @Override
    public Printer toModel(final PrinterDTO dto) {
        Printer model;
        if (dto.isNew()) {
            model = new Printer();
        } else {
            model = this.printerRepository.getOne(dto.getId());
        }

        model.setIp(dto.getIp());
        model.setPort(dto.getPort());
        model.setBelongs(dto.getBelongs());
        final AuthOganization org = this.authOganizationRepository.findByCode(dto.getDepartmentCode());
        model.setDepartmentCode(org.getCode());
        model.setDepartmentName(org.getName());
        model.setLabelTitle(dto.getLabelTitle());
        model.setLeftMargin(dto.getLeftMargin());
        return model;
    }

    @Override
    public PrinterDTO toDTO(final Printer model, final boolean forListView) {
        final PrinterDTO dto = new PrinterDTO();
        dto.setId(model.getId());
        dto.setIp(model.getIp());
        dto.setPort(model.getPort());
        dto.setBelongs(model.getBelongs());
        dto.setDepartmentCode(model.getDepartmentCode());
        dto.setDepartmentName(model.getDepartmentName());
        dto.setLabelTitle(model.getLabelTitle());
        dto.setLeftMargin(model.getLeftMargin());
        return dto;
    }

}
